<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'老师列表'),
	array('op'=>'edit','title'=>'添加老师'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','name','storeid','lessonid','label','status','show','mid','headimg','remark','years','age','createtime','classid');
	$list=pdo_getslice('jy_edu_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['store']=pdo_get('jy_edu_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']));
				$item['lesson']=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['lessonid']));
				$item['classtitle']=pdo_getcolumn('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classid']),'title');
			}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'老师名称','type'=>'string'),
			array('name'=>'headimg','title'=>'老师头像','type'=>'image'),
			array('name'=>'store.title','title'=>'所属门店','type'=>'string'),
			array('name'=>'classtitle','title'=>'所属班级','type'=>'string'),
			array('name'=>'lesson.title','title'=>'课程','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
		)),
		'search_one'=>array('name'=>'name'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_edu_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回老师列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'老师姓名','type'=>'string'),
					array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'store'),
					array('name'=>'classid','title'=>'所属班级','type'=>'select','options'=>'class'),
					array('name'=>'lessonid','title'=>'课程','type'=>'select','options'=>'lesson'),
					array('name'=>'headimg','title'=>'头像','type'=>'image'),
					array('name'=>'time','title'=>'上课时间','type'=>'time_hour'),
					array('name'=>'remark','title'=>'简要描述','type'=>'text'),
					array('name'=>'show','title'=>'详细介绍','type'=>'richtext'),
					array('type'=>'choose','title'=>'绑定会员','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
				//	array('name'=>'person_limit','title'=>'上课人数','type'=>'string'),
				//	array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['class']=pdo_getall('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['lesson']=pdo_getall('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['store']=pdo_getall('jy_edu_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['storeid']){
		exi('请选择门店','error');
	}
	if(!$postdata['classid']){
		exi('请选择班级','error');
	}
	if(!$postdata['lessonid']){
		exi('请选择课程','error');
	}
	//$postdata['time']=ijson_encode($postdata['time']);	
	$data=array_fields(array('name','headimg','show','remark','paixu','status','lessonid','storeid','mid','classid'),$postdata);
	$id=intval($postdata['id']);
	
	if($id){
		pdo_update('jy_edu_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_edu_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_edu_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的老师！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_edu_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}